import time

import pyautogui

from src.python.location import resolutions
from src.python.tools.get_all_windows import getAllWindow
from src.python.tools.logHandler import logger

log = logger()


def startFuhua(resolution_select):
    # 获取全部风云窗口
    windows = getAllWindow()
    from src.python.gui.page.fuhau_page import FuhuaPage
    if not windows:
        FuhuaPage().showNoFindWindowBox()
    else:
        # 遍历窗口
        for window in windows:
            log.info(f"开始{window}窗口孵化")
            # 判断窗口是否最小化,如果是则恢复
            if window.isMinimized:
                window.restore()
            # 判断窗口是否激活,如果未激活则激活
            if not window.isActive:
                logger().info("判断窗口是否激活,如果未激活则激活")
                window.activate()
            # 获取当前窗口宽 高
            weight, height = window.width, window.height
            # 移动窗口到左上角
            window.moveTo(0, 0)
            # 设置窗口大小
            window.resizeTo(800, 600)
            # 获取分辨率坐标
            resolution_class = getattr(resolutions, resolution_select)
            resolution = resolution_class()

            # 点击操作
            click(resolution.ling)
            click(resolution.jingpozhuanhua)
            click(resolution.baowufuhua)
            click(resolution.kaishifuhua)
            click(resolution.kaishifuhua)
            click(resolution.kaishifuhua)
            click(resolution.queding)
            click(resolution.queding)
            click(resolution.ling)

            # 恢复原来大小
            # weight, height = (int)((1024 * 1.25) + 20), (int)((768 * 1.25) + 49)
            window.resizeTo(weight, height)
            window.minimize()
            log.info(f"{window}窗口孵化完成,即将开始下个窗口孵化")
        FuhuaPage().fuhuaTimerTask()


def click(location):
    from src.python.gui.page.fuhau_page import FuhuaPage
    click_interval = FuhuaPage().get_click_interval()
    pyautogui.click(location)
    time.sleep(click_interval)